import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/table';
import { goodsList, setIsShelf, setIsShowShop } from '@apps/ant-bus/src/views/business/goods/goods/api';
import { vipcardList } from '@apps/ant-bus/src/views/business/goods/vipcard/data/api';
import { h } from 'vue';
import { Switch, Tag } from 'ant-design-vue';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import { rules } from '@pkg/utils';
import { employeeList } from '@apps/ant-bus/src/views/business/employeeManage/employee/data/api';
import { weChatList } from '@apps/ant-bus/src/views/business/platformManage/wechat/data/api';

export const columns: BasicColumn[] = [
  {
    title: '首字母',
    dataIndex: 'firstLetter',
    width: 100,
  },
  {
    title: '通道名称',
    dataIndex: 'goodsName',
    width: 180,
  },
  {
    title: '营业时间',
    dataIndex: 'viponlyTime',
    width: 120,
  },
  {
    title: '联系电话',
    dataIndex: 'viponlyPhone',
    width: 120,
  },
  {
    title: '市场价格(元)',
    dataIndex: 'goodsPrice',
    width: 100,
  },
  {
    title: '会员价格(元)',
    dataIndex: 'vipPrice',
    width: 100,
  },
  {
    title: '通道图片',
    dataIndex: 'coverImg',
    width: 100,
    customRender: render.renderImage,
  },
  {
    title: '是否需要预约',
    dataIndex: 'isAppoint',
    width: 100,
    customRender: ({ record }: Recordable) => {
      const isAppoint = record.isAppoint;
      const color = isAppoint == 1 ? 'green' : 'red';
      const text = isAppoint == 1 ? '不需要' : '需要';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '预约控制(天)',
    dataIndex: 'maxTime',
    width: 80,
  },
  {
    title: '上下架',
    dataIndex: 'isShelf',
    width: 100,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShelf,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '已上架',
        unCheckedChildren: '已下架',
        loading: record.pendingStatus,
        onChange(checked: number) {
          console.info(checked);
          record.pendingStatus = false;
          setIsShelf(record.viponlyId, checked)
            .then(data => {
              record.isShelf = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '商城展示',
    dataIndex: 'isShowShop',
    width: 90,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShowShop,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '展示',
        unCheckedChildren: '不展示',
        loading: record.pendingStatus,
        onChange(checked: number) {
          record.pendingStatus = false;
          setIsShowShop(record.viponlyId, checked)
            .then(data => {
              record.isShowShop = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '通道地址',
    dataIndex: 'viponlyAddress',
    width: 150,
  },
  {
    title: '温馨提示',
    dataIndex: 'viponlyTips',
    width: 100,
  },
  {
    title: '有效期(天)',
    dataIndex: 'validDay',
    width: 100,
    align: 'center',
  },
  {
    title: '排序',
    dataIndex: 'viponlySort',
    width: 80,
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 120,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'goodsName',
    label: '通道名称',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'isShelf',
    label: '是否上架',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '上架', value: 1 },
        { label: '下架', value: 2 },
      ],
    },
  },
  {
    field: 'isIntegral',
    label: '是否支持兑换',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '支持兑换', value: 1 },
        { label: '不支持兑换', value: 2 },
      ],
    },
  },
  {
    field: 'weChatId',
    label: '上架公众号',
    component: 'ApiSelect',
    colProps: { span: 5 },
    componentProps: {
      api: weChatList,
      params: {},
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
    },
  },
  {
    field: 'isAppoint',
    label: '需要预约',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '不需要', value: 1 },
        { label: '需要', value: 2 },
      ],
    },
  },
  {
    field: 'checkEmployeeId',
    label: '所属负责人',
    component: 'ApiSelect',
    colProps: { span: 5 },
    componentProps: {
      api: employeeList,
      params: {
        handlerName: 'busEmployeeListHandler',
        employeeType: 'platform',
        employeePosition: 'manager',
      },
      labelField: 'employeeAccountName',
      valueField: 'employeeId',
      showSearch: true,
      immediate: false,
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'divider1',
    label: '贵宾通道基础信息',
    component: 'Divider',
    helpMessage: '贵宾通道基础信息',
  },
  {
    label: '贵宾通道Id',
    field: 'viponlyId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '机场Id',
    field: 'airportId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '首字母',
    field: 'firstLetter',
    component: 'Input',
    required: true,
  },
  {
    label: '通道名称',
    field: 'goodsName',
    component: 'Input',
    required: true,
  },
  {
    label: '营业时间',
    field: 'viponlyTime',
    component: 'Input',
    required: true,
  },
  {
    label: '联系电话',
    field: 'viponlyPhone',
    component: 'Input',
    required: false,
  },
  {
    label: '市场价格',
    field: 'goodsPrice',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '会员价格',
    field: 'vipPrice',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '成本价格',
    field: 'costPrice',
    component: 'InputNumber',
    required: false,
  },
  {
    label: '销量',
    field: 'sales',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '库存',
    field: 'inventory',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '排序',
    field: 'viponlySort',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '温馨提示',
    field: 'viponlyTips',
    component: 'InputTextArea',
    required: false,
  },
  {
    label: '通道地址',
    field: 'viponlyAddress',
    component: 'Input',
    required: true,
  },
  {
    label: '航站楼标签',
    field: 'tagTerminal',
    component: 'Input',
    required: false,
  },
  {
    label: '所属贵宾厅',
    field: 'viproomId',
    component: 'ApiSelect',
    required: false,
    componentProps: () => {
      return {
        api: goodsList,
        params: { handlerName: 'busGoodsListHandler', categoryCode: 'viproom' },
        labelField: 'goodsName',
        valueField: 'goodsId',
        immediate: true,
        showSearch: true,
      };
    },
  },
  {
    label: '商城展示',
    field: 'isShowShop',
    component: 'Select',
    required: false,
    show: false,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不展示', value: 2 },
        { label: '展示', value: 1 },
      ],
    },
  },
  {
    label: '扫码说明',
    field: 'useDesc',
    component: 'InputTextArea',
    required: false,
  },
  {
    label: '封面图',
    field: 'coverImg',
    component: 'ImageUpload',
    required: false,
  },
  {
    label: '轮播图',
    field: 'goodsImg',
    component: 'ImageUpload',
    required: false,
    componentProps: {
      fileMax: 5,
    },
  },
  {
    label: '通道简介',
    field: 'goodsDesc',
    component: 'TyEditor',
  },
  {
    field: 'divider2',
    label: '合作企业贵宾通道信息',
    component: 'Divider',
    helpMessage: '合作企业贵宾通道信息',
  },
  {
    label: '营业起时间',
    field: 'startTime',
    component: 'Input',
    required: false,
  },
  {
    label: '营业止时间',
    field: 'endTime',
    component: 'Input',
    required: false,
  },
  {
    label: '出发类型',
    field: 'departType',
    component: 'Select',
    required: false,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '国外出发', value: 2 },
        { label: '国内出发', value: 1 },
      ],
    },
  },
  {
    label: '航站楼信息',
    field: 'terminalInfo',
    component: 'Input',
    required: false,
  },
  {
    field: 'divider3',
    label: '贵宾通道设置',
    component: 'Divider',
    helpMessage: '贵宾通道设置',
  },
  {
    label: '所属负责人',
    field: 'checkEmployeeId',
    component: 'ApiSelect',
    required: false,
    componentProps: ({ formModel }) => {
      return {
        api: employeeList,
        params: {
          handlerName: 'busEmployeeListHandler',
          employeeType: 'platform',
          employeePosition: 'manager',
        },
        labelField: 'employeeAccountName',
        valueField: 'employeeId',
        showSearch: true,
        immediate: true,
        onChange: employeeId => {
          formModel.serverId = null;
        },
      };
    },
  },
  {
    label: '是否上架',
    field: 'isShelf',
    component: 'Select',
    required: true,
    defaultValue: 2,
    dynamicDisabled: true,
    componentProps: {
      options: [
        { label: '不上架', value: 2 },
        { label: '上架', value: 1 },
      ],
    },
  },
  {
    label: '推荐卡种',
    field: 'vipcardId',
    component: 'ApiSelect',
    required: false,
    componentProps: {
      api: vipcardList,
      params: { handlerName: 'busVipCardListHandler' },
      labelField: 'goodsName',
      valueField: 'goodsId',
      immediate: false,
      showSearch: true,
    },
  },
  {
    label: '需要预约',
    field: 'isAppoint',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '需要', value: 2 },
        { label: '不需要', value: 1 },
      ],
    },
  },
  {
    label: '预约控制(天)',
    field: 'maxTime',
    component: 'InputNumber',
    required: true,
    ifShow: ({ model }) => {
      if (model.isAppoint == 2) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '预约开始时间',
    field: 'appointmentStartTime',
    component: 'TimePicker',
    required: true,
    componentProps: {
      valueFormat: 'HH:mm',
      format: 'HH:mm',
    },
    ifShow: ({ model }) => {
      if (model.isAppoint == 2) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '预约结束时间',
    field: 'appointmentEndTime',
    component: 'TimePicker',
    required: true,
    componentProps: {
      valueFormat: 'HH:mm',
      format: 'HH:mm',
    },
    ifShow: ({ model }) => {
      if (model.isAppoint == 2) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '有效期',
    field: 'isHaveValidDay',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '无有效期', value: 2 },
        { label: '有有效期', value: 1 },
      ],
    },
  },
  {
    label: '有效期(天)',
    field: 'validDay',
    component: 'InputNumber',
    required: true,
    defaultValue: 60,
    ifShow: ({ model }) => {
      if (model.isHaveValidDay == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    field: 'divider4',
    label: '代理商售卖信息设置',
    component: 'Divider',
    helpMessage: '代理商售卖信息设置',
  },
  {
    label: '代理商价格',
    field: 'agentPrice',
    component: 'InputNumber',
    required: true,
  },
  {
    label: '上架到代理商',
    field: 'isShelfAgent',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不上架', value: 2 },
        { label: '上架', value: 1 },
      ],
    },
  },
  {
    field: 'divider5',
    label: '电子金兑换设置',
    component: 'Divider',
    helpMessage: '电子金兑换设置',
  },
  {
    label: '是否支持电子金兑换',
    field: 'isIntegral',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '支持', value: 1 },
        { label: '不支持', value: 2 },
      ],
    },
  },
  {
    label: '所需电子金(整数)',
    field: 'integralValue',
    component: 'InputNumber',
    defaultValue: 2,
    required: true,
    dynamicRules: () => rules.price(true),
  },
];
